﻿#include "bigtoolbutton.h"
#include <QPainter>
#include <QMouseEvent>

BigToolButton::BigToolButton(QString &image_path, QWidget *parent)
    : QToolButton(parent)
{

    m_mouse_over = false;
    m_is_checked = false;

    // 开关式按钮，相对的是触发按钮.
    setCheckable(true);

    //设置文本颜色
    setTextColor(QColor(230, 230, 230));

    //设置文本粗体
    QFont &text_font = const_cast<QFont &>(font());
    text_font.setWeight(QFont::Bold);

    setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    //设置图标
    QPixmap pixmap(image_path);
    setIcon(pixmap);
    setIconSize(pixmap.size());

    //设置id
    setObjectName("transparentToolButton");

    createConnections();
}

void BigToolButton::enterEvent(QEvent *)
{
    m_mouse_over = true;
}

void BigToolButton::leaveEvent(QEvent *)
{
    m_mouse_over = false;
}

void BigToolButton::setTextColor(QColor &color)
{
    QPalette text_palette = palette();
    text_palette.setColor(QPalette::ButtonText, color);
    setPalette(text_palette);
}

void BigToolButton::paintEvent(QPaintEvent *event)
{
    if (m_mouse_over)
    {
        //painterShadow(0, 100, 150);
        highLightBg(QString("rgb(77, 177, 252)"));
    }
    else if (m_is_checked)
    {
        //painterShadow(0, 100, 150);
        highLightBg(QString("rgb(61, 165, 252)"));
    }
    else
    {
        highLightBg(QString("transparent"));
    }

    QToolButton::paintEvent(event);
}

/**
 * @brief 渐变光效果.
 * @param top_color
 * @param middle_color
 * @param bottom_color
 */
void BigToolButton::painterShadow(int top_color, int middle_color, int bottom_color)
{
    QPainter painter(this);
    QPen pen(Qt::NoBrush, 1);
    painter.setPen(pen);
    QLinearGradient linear(rect().topLeft(), rect().bottomLeft());
    linear.setColorAt(0, QColor(230, 230, 230, top_color));
    linear.setColorAt(0.5, QColor(230, 230, 230, middle_color));
    linear.setColorAt(1, QColor(230, 230, 230, bottom_color));
    painter.setBrush(linear);
    painter.drawRect(rect());
}

void BigToolButton::highLightBg(QString &str_color)
{
    setStyleSheet("background:" + str_color);
}

void BigToolButton::onToggled(bool is_checked)
{
    m_is_checked = is_checked;
}

void BigToolButton::createConnections()
{
    connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
}

